/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_teleport.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_map_window.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "army.h"
#include "compressed_filter.h"
#include "game_time.h"
#include "mini_map_window.h"
#include "saved_game_header.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_teleport::t_adventure_event_teleport ( ) 
: t_adventure_event_base ( k_teleport_event ) 
{ 	
}

t_adventure_event_teleport::t_adventure_event_teleport ( t_adventure_object* adv_object, t_adv_map_point point_a , t_level_map_point_2d point_b ) 
: t_adventure_event_base ( k_teleport_event ) 
{ 
	m_global_id		= adv_object->get_global_id();
	m_event_point_a	= point_a;
	m_event_point_b	= point_b;
}

void t_adventure_event_teleport::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{	
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
	
	t_army* army = dynamic_cast<t_army*>(object);
	
	if (army == NULL) 
		return;
	
	t_level_map_point_2d	army_position = army->get_position();

	army->get_map()->get_map_window()->center_view(m_event_point_b);

/*
	if (army_position.level != m_event_point_b.level)
	{
		//common' lets the ride the choo choo train! 8^)
		army->get_map()->get_map_window()->get_frame()->get_mini_map_window()->set_view_level(m_event_point_b.level);
	}
*/
	
	army->move(m_event_point_b);

	return;
}

void t_adventure_event_teleport::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{	
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
	
	t_army* army = dynamic_cast<t_army*>(object);
	
	if (army == NULL) 
		return;
	
	army->teleport(m_event_point_a);

	return;
}

bool t_adventure_event_teleport::read( std::streambuf & buffer , int version )
{	
	if (version >= 30)
	{
		buffer >> m_event_point_a;
	}
	else
	{
		t_level_map_point_2d	begin;

		buffer >> begin;

		(t_level_map_point_2d)m_event_point_a	= begin;
	}

	buffer >> m_event_point_b ;

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_teleport::write( std::streambuf & buffer )
{
	buffer << m_event_point_a ;
	buffer << m_event_point_b ;

	return t_adventure_event_base::write( buffer );
}
